package br.com.medmark.model.hibernate.dao;

import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import br.com.medmark.model.hibernate.Solicitacao;
import br.medmark.util.Constantes;

public class SolicitacaoDAO extends GenericoDAOImpl<Solicitacao>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 604745553187719666L;
	
	private static SolicitacaoDAO solicitacaoDAO = null;
	
	
	private SolicitacaoDAO(){
		
	}
	
	public static SolicitacaoDAO getInstance(){
		if (solicitacaoDAO == null) {
			solicitacaoDAO = new SolicitacaoDAO();
		}
		
		return solicitacaoDAO;
	}
	
	
	public List<Solicitacao> getSolicitacoes(int codigoClinica,int status) {
		String sql = "SELECT s FROM Solicitacao s , Horario h Where s.horario.codigo = h.codigo and h.clinica.codigo = :codigoClinica";
		
		if (status == Constantes.STATUS_SOLICITACAO_ACEITA || status == Constantes.STATUS_SOLICITACAO_PENDENTE
				|| status == Constantes.STATUS_SOLICITACAO_REJEITADA) {
			
			 sql = sql + " and s.statusSolicitacao = :status";
			
		}
		
		
		TypedQuery<Solicitacao> query = this.entityManager.createQuery(sql, Solicitacao.class);
		if (status != Constantes.STATUS_SOLICITACAO_TODOS) {
			query.setParameter("status", status);
		}
		query.setParameter("codigoClinica", codigoClinica);

		
		try {
			return query.getResultList();
		} catch (NoResultException ex) {
			return null;
		}
	}

}
